<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>添加商品-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../static/css/font.css">
    <link rel="stylesheet" href="../../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {
            -webkit-appearance: none;
            appearance: none;
            margin: 0;
        }
    </style>
</head>

<body>
<div class="weadmin-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="L_username" class="layui-form-label">
                <span class="we-red">*</span>商品名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_username" lay-verify="required" name="name" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="we-red">*</span>商品类型
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_phone" lay-verify="required" name="category" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="we-red">*</span>商品价格
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_price" lay-verify="required|price" name="price" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="add" lay-submit="">确定</button>
        </div>
    </form>
</div>
<script src="../../../static/lib/layui/layui.js" charset="utf-8"></script>

<script>
    layui.extend({
        admin: '{/}../../static/js/admin'
    });
    layui.use(['form', 'jquery', 'util', 'admin', 'layer'], function () {
        var form = layui.form,
            $ = layui.jquery,
            util = layui.util,
            admin = layui.admin,
            layer = layui.layer;

        //自定义验证规则
        form.verify({
            price: [/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/, '价格只能有数字，且最多两位小数']
        });

        //监听提交
        form.on('submit(add)', function (data) {
            var f = data.field;
            var product_data = {productName: f.name,category: f.category,price: f.price}
            $.post({
                url: "/product/add",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(product_data),
                dataType: "json",
                success: function (data) {
                    var length = parent.$("tbody").children("tr").length
                    var num = parseInt(parent.$(".fr>span").html())
                    $(".fr>span", window.parent.document).html(++num);
                    if (length<10) {
                        var date  = new Date()
                        date = dateFormat("YYYY-mm-dd HH:MM:SS",date)
                        parent.$('#productList').append(
                            '<tr data-id="' + data.productId + '">' +
                            '<td>' +
                            '<div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id="' + data.productId + '"><i class="layui-icon">&#xe605;</i></div>' +
                            '</td>' +
                            '<td>' + data.productId + '</td>' +
                            '<td>' + f.name + '</td>' +
                            '<td>' + f.category + '</td>' +
                            '<td>' + f.price + '</td>' +
                            '<td>' + date + '</td>' +
                            '<td class="td-status">\n' +
                            '            <span class="layui-btn layui-btn-normal layui-btn-xs">已启用</span>\n' +
                            '          </td>\n' +
                            '          <td class="td-manage">\n' +
                            '            <a onclick="member_stop(this,\'10001\')" href="javascript:;" title="启用">\n' +
                            '              <i class="layui-icon layui-icon-download-circle"></i>\n' +
                            '            </a>\n' +
                            '            <a title="编辑" onclick="WeAdminEdit(\'编辑\',\'./edit.html\', 1, 600, 400)" href="javascript:;">\n' +
                            '              <i class="layui-icon layui-icon-edit"></i>\n' +
                            '            </a>\n' +
                            '            <a title="删除" onclick="member_del(this)" href="javascript:;">\n' +
                            '              <i class="layui-icon layui-icon-delete"></i>\n' +
                            '            </a>\n' +
                            '          </td>' +
                            '</tr>'
                        );
                    }
                }
            })
            layer.alert("增加成功", {
                icon: 6
            })
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
            return false;
        });

    });

    function dateFormat(fmt, date) {
        let ret;
        const opt = {
            "Y+": date.getFullYear().toString(),        // 年
            "m+": (date.getMonth() + 1).toString(),     // 月
            "d+": date.getDate().toString(),            // 日
            "H+": date.getHours().toString(),           // 时
            "M+": date.getMinutes().toString(),         // 分
            "S+": date.getSeconds().toString()          // 秒
            // 有其他格式化字符需求可以继续添加，必须转化成字符串
        };
        for (let k in opt) {
            ret = new RegExp("(" + k + ")").exec(fmt);
            if (ret) {
                fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
            };
        };
        return fmt;
    }
</script>
</body>

</html>